package net.cdsunrise.com.auth.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author: LiuWei
 * @date: Create in 10:36 2018/05/24
 */
//@Configuration
public class RequestInterceptorConfig implements RequestInterceptor {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    private static final String AUTHORIZATION_HEADER = "Authorization";

    private static final String BEARER_TOKEN_TYPE = "Bearer";

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        String accessToken = attributes.getRequest().getHeader(AUTHORIZATION_HEADER);
        logger.debug("RequestInterceptorConfig accessToken :" + accessToken);
//        requestTemplate.header(AUTHORIZATION_HEADER,
//                String.format("%s %s",
//                        BEARER_TOKEN_TYPE,
//                        accessToken));
        requestTemplate.header(AUTHORIZATION_HEADER, accessToken);
    }
}
